package com.jidong.test_springboot.aop;

import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.jidong.test_springboot.dao.DictDao;
import com.jidong.test_springboot.entity.UserWithDept;
import com.jidong.test_springboot.vo.ResultData;

@Aspect
@Component
public class AutoDict {

	@Autowired
	private DictDao dictDao; 
	
	
	@Pointcut("execution(public * com.jidong.test_springboot.web.*Servlet.*(..))")
	public void pointCut() {

	}

	// 自定义的切面执行内容，
	@Around(value = "pointCut()")
	public Object around(ProceedingJoinPoint pjp) throws Throwable {
		//找到当前方法的返回结果
		Object obj=pjp.proceed();//正常执行该方法
		//开始解析obj
		if(obj instanceof ResultData) {
			ResultData result=(ResultData)obj;
			if(result.getResult() instanceof JSONObject) {
				JSONObject record=(JSONObject)result.getResult();
				Object list=record.get("data");
				if(list!=null && list instanceof List) {
					List t=(List)list;
					for(int i=0;i<t.size();i++) {
						if(t.get(i) instanceof UserWithDept) {
							UserWithDept u=(UserWithDept)t.get(i);
							String key=u.getGender();
							String value=dictDao.getValue("gender",key).getDictValue();
							u.setGender(value);
						}
					}
				}
			}
		}
		return obj;
	}

}
